package com.example.msgBoard.service.impl;

import com.example.msgBoard.mapper.UserMapper;
import com.example.msgBoard.model.User;
import com.example.msgBoard.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public User registerUser(User user) throws Exception {
        if (findByUsername(user.getUsername()) != null) {
            throw new Exception("用户名已存在");
        }
        user.setRole("USER"); // 设置默认角色为普通用户
        userMapper.insertUser(user);
        return user;
    }

    // 实现其他必要的方法
}
